// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// See License.txt in the project root for license information.
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
//using System.Windows.Shapes; // Avoid conflict with System.IO.Path.

namespace ActorFramework.Fakebook
{
    /// <summary>
    /// Interaction logic for NewUser.xaml
    /// </summary>
    public partial class EditProfile : Window
    {
        private bool _newUser;

        public EditProfile(bool newUser)
        {
            InitializeComponent();
            _newUser = newUser;

            FakebookPerson client = App.FakebookClient;
            if (newUser)
            {
                WelcomeTextBlock.Text = String.Format("Welcome to Fakebook, {0}!", client.FirstName);
            }
            else
            {
                WelcomeTextBlock.Text = "Edit your Fakebook Profile";

                // Set values based on current profile info
                if (client.Gender == Gender.Female)
                    FemaleRadio.IsChecked = true;

                CityTextBox.Text = client.City;

                switch (client.RelationshipStatus)
                {
                    case RelationshipStatus.Single:
                        SingleRadio.IsChecked = true;
                        break;
                    case RelationshipStatus.Married:
                        MarriedRadio.IsChecked = true;
                        break;
                    case RelationshipStatus.Engaged:
                        EngagedRadio.IsChecked = true;
                        break;
                    case RelationshipStatus.Divorced:
                        DivorcedRadio.IsChecked = true;
                        break;
                    case RelationshipStatus.Unspecified:
                        break;
                    default:
                        Contract.Assume(false, "Unrecognized RelationshipStatus value");
                        throw new NotSupportedException("Unrecognized RelationshipStatus value");
                }
            }
        }

        private void DoneButton_Click(object sender, RoutedEventArgs e)
        {
            FakebookPerson client = App.FakebookClient;

            // Read data from the form
            Gender gender = Gender.Unspecified;
            if (MaleRadio.IsChecked.GetValueOrDefault())
                gender = Gender.Male;
            else if (FemaleRadio.IsChecked.GetValueOrDefault())
                gender = Gender.Female;

            String cityName = CityTextBox.Text;
            DateTimeOffset birthday = DateTimeOffset.MinValue;
            if (!String.IsNullOrWhiteSpace(BirthdayTextBox.Text))
                DateTimeOffset.TryParse(BirthdayTextBox.Text, out birthday);

            RelationshipStatus relStatus = RelationshipStatus.Unspecified;
            if (SingleRadio.IsChecked.GetValueOrDefault())
                relStatus = RelationshipStatus.Single;
            else if (MarriedRadio.IsChecked.GetValueOrDefault())
                relStatus = RelationshipStatus.Married;
            else if (EngagedRadio.IsChecked.GetValueOrDefault())
                relStatus = RelationshipStatus.Engaged;
            else if (DivorcedRadio.IsChecked.GetValueOrDefault())
                relStatus = RelationshipStatus.Divorced;

            String profilePicturePath = null;
            if (!String.IsNullOrWhiteSpace(ProfilePictureTextBox.Text))
                profilePicturePath = ProfilePictureTextBox.Text;

            // Now for anything we've configured, set that data
            if (gender != Gender.Unspecified && client.Gender != gender)
                client.Gender = gender;

            if (!String.IsNullOrWhiteSpace(cityName) && client.City != cityName)
                client.City = cityName;

            if (birthday != DateTimeOffset.MinValue && client.Birthday != birthday)
                client.Birthday = birthday;

            if (relStatus != RelationshipStatus.Unspecified && client.RelationshipStatus != relStatus)
                client.RelationshipStatus = relStatus;

            if (profilePicturePath != null)
            {
                byte[] imageBytes = File.ReadAllBytes(profilePicturePath);
                client.ProfilePicture = new PictureInfo(Path.GetFileName(profilePicturePath), imageBytes, null, null, null, null);
            }

            if (_newUser)
            {
                var newPage = new UserHomePage(App.AccountName);
                this.Hide();
                newPage.Show();
            }
            this.Close();
        }

        private void BrowseButton_Click(object sender, RoutedEventArgs e)
        {
            String fileName = null;
            var dialog = new OpenFileDialog();
            dialog.Filter = "Images|*.png;*.jpg;*.gif;*.bmp|All files|*";
            dialog.Title = "Select a profile picture!";
            bool? r = dialog.ShowDialog();
            if (r.GetValueOrDefault())
            {
                fileName = dialog.FileName;
                ProfilePictureTextBox.Text = fileName;
            }
        }
    }
}
